package com.itheima.redbaby.net;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.itheima.redbaby.ContantValue;
import com.itheima.redbaby.GloableParams;

import android.net.Proxy;
import android.text.TextUtils;

/**
 * 联网工具
 * 
 * @author l
 * 
 */
public class HttpUtil {
	private HttpClient client;

	private HttpPost post;
	private HttpGet get;

	public HttpUtil() {
		client = new DefaultHttpClient();
		if (GloableParams.isWap) {
			// 设置代理
			String ip = Proxy.getDefaultHost();
			int port = Proxy.getDefaultPort();
			HttpHost value = new HttpHost(ip, port);

			client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
					value);
		}
	}

	/**
	 * 发送xml到服务器端
	 * 
	 * @param url
	 * @param xml
	 * @return
	 */
	public InputStream sendXml(String url, String xml) {
		post = new HttpPost(url);
		try {
			StringEntity entity = new StringEntity(xml, ContantValue.ENCODING);
			post.setEntity(entity);

			HttpResponse response = client.execute(post);
			if (response != null
					&& response.getStatusLine().getStatusCode() == 200) {
				return response.getEntity().getContent();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 发送post请求
	 * 
	 * @param url
	 * @param params
	 * @return
	 */
	public String sendPost(String url, Map<String, Object> params, String userid) {
		post = new HttpPost(url);

		// 数据设置
		if (params != null && params.size() > 0) {
			List<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>();
			for (Map.Entry<String, Object> item : params.entrySet()) {
				parameters.add(new BasicNameValuePair(item.getKey(), item
						.getValue().toString()));
			}
			
			try {
				UrlEncodedFormEntity entity = new UrlEncodedFormEntity(
						parameters, ContantValue.ENCODING);
				post.setEntity(entity);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if (!TextUtils.isEmpty(userid)) {
			post.setHeader("userid", userid);
		}

		try {
			HttpResponse response = client.execute(post);
			if (response != null
					&& response.getStatusLine().getStatusCode() == 200) {
				// response.getEntity().getContent() InputStream
				return EntityUtils.toString(response.getEntity(),
						ContantValue.ENCODING);

			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	/**
	 * 发送get请求
	 * 
	 * @param url
	 * @param params
	 * @return
	 */
	public String sendGet(String url, Map<String, Object> params,String userid) {
		// http://localhost:8080/help?version=1&

		
	/*	if(params!=null){
			url += "?";
			for (Map.Entry<String, Object> item : params.entrySet()) {
				url += item.getKey() + "=" + item.getValue() + "&";
			}
			url = url.substring(0, url.length() - 1);*/

		if (params != null && params.size() > 0) {
			url += "?";
			for (Map.Entry<String, Object> item : params.entrySet()) {
				url += item.getKey() + "=" + item.getValue() + "&";
			}
			url = url.substring(0, url.length() - 1);

		}
		get = new HttpGet(url);
		
		if (!TextUtils.isEmpty(userid)) {
			get.setHeader("userid", userid);
		}

		try {
			HttpResponse response = client.execute(get);
			if (response != null
					&& response.getStatusLine().getStatusCode() == 200) {
				// response.getEntity().getContent() InputStream
				return EntityUtils.toString(response.getEntity(),
						ContantValue.ENCODING);

			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;

	}
}
